package com.huizhonglm.diap.ui.user;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.bruce.common.ActivityMgr;
import com.bruce.common.utils.EmptyUtil;
import com.bruce.common.utils.PatternUitl;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.utils.DialogUtil;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.presenters.user.PasswdResetPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.user.iView.PwdResetIView;


/**
 * 重置密码
 *
 * @author LiXiaolong
 * @version 1.0
 * @since 2014年6月5日下午1:55:43
 */
public class PasswdResetActivity extends BaseActivity implements PwdResetIView, View.OnClickListener {
    /**
     * 新密码
     */
    private EditText newPasswd = null;
    /**
     * 重复密码
     */
    private EditText repPasswd = null;

    private String phoneNumber;

    private PasswdResetPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_passwd_reset);
        setBackEvent(this, null);
        mPresenter = new PasswdResetPresenter(this);
        assignViews();
    }

    @Override
    protected void assignViews() {
        phoneNumber = getIntent().getExtras().getString("phoneNumber");
        newPasswd = (EditText) findViewById(R.id.new_passwd_edt);
        repPasswd = (EditText) findViewById(R.id.rep_passwd_edt);
        View resetBtn = findViewById(R.id.reset_pwd_btn);

        resetBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reset_pwd_btn:
                String newPwd = newPasswd.getText().toString().trim();
                String repPwd = repPasswd.getText().toString().trim();
                if (checkPwd(newPwd, repPwd)) {
                    closeKeyboard(PasswdResetActivity.this);
                    showProgressBar();
                    mPresenter.resetPasswd(phoneNumber, newPwd);
                }
                break;
        }
    }

    @Override
    public void onResetSuccess() {
        hideProgressBar();
        DialogUtil.showConfirmDialog(this, "提示", "重置密码成功", new DialogUtil.OnDialogBtnClickListener() {
            @Override
            public void onBtnClick(String btnText) {
                ActivityMgr.getInstance().popActivity(PasswdForgetActivity.class);
                onBackPressed();
            }
        });
    }

    @Override
    public void onResetFailure() {
        hideProgressBar();
       ToastUtil.makeTextShort("密码重置失败");
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    private boolean checkPwd(String newPwd, String repPwd) {
        if (EmptyUtil.isEmpty(newPwd)) {
            ToastUtil.makeTextShort("请输入密码");
            return false;
        }else {
            if (newPwd.length() < 6) {
                ToastUtil.makeTextShort("密码长度不够");
                return false;
            }
            if (PatternUitl.isPureNum(newPwd)) {
                ToastUtil.makeTextShort("对不起，密码不能为纯数字");
                return false;
            }
        }

        if (EmptyUtil.isEmpty(repPwd)) {
            ToastUtil.makeTextShort("请输入确认密码");
            return false;
        }

        if (!newPwd.equals(repPwd)) {
            ToastUtil.makeTextShort("两次输入的密码不一致");
            return false;
        }
        return true;
    }
}
